//
//  YiDongWebViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "YiDongWebViewController.h"
#import "YiDongImgModel.h"
#import "YiDongWebViewModel.h"
#import "UMSocial.h"


@interface YiDongWebViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)YiDongWebViewModel *YiDongModel;


@end

@implementation YiDongWebViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.webView.delegate = self;
    _webView.scrollView.showsHorizontalScrollIndicator = NO;
    _webView.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.webView];
    [self getRequest];
    // 设置导航栏
    [self loadNavigationIndfo];
    self.hidesBottomBarWhenPushed = YES;
}

#pragma - mark - 字体设置
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    //字体大小
    CGFloat font = [[NSUserDefaults standardUserDefaults] floatForKey:@"fontSize"];

    if ((int)(font/14*100) == 0) {
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
        return;
    }
    NSString *string = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",(int)(font/14*100)];
    [webView stringByEvaluatingJavaScriptFromString:string];
}

#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    self.navigationController.navigationBar.translucent = NO;
    
    // 设置导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    
    // 返回
    UIBarButtonItem *leftBackButton = [UIBarButtonItem itemWithTarget:self Action:@selector(backAction) image:@"home_back" highImage:@"home_back_highlighted"];
    self.navigationItem.leftBarButtonItem = leftBackButton;
    
    // 评论
    UIBarButtonItem *rightSaveButton  = [UIBarButtonItem itemWithTarget:self Action:@selector(shareBtnAction) image:@"contenttoolbar_hd_share_light" highImage:@"contenttoolbar_hd_share"];

    self.navigationItem.rightBarButtonItem = rightSaveButton;
    
    // 标题
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

#pragma mark - 返回按钮触发事件
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 分享按钮触发事件
- (void)shareBtnAction
{
    // 分享
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55a8c25567e58e15d8000cb2" shareText:[NSString stringWithFormat:@"%@ | 【IT家园】猛戳查看详情%@",self.Mytitle,self.url_3w] shareImage:self.myImage shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToQQ,UMShareToSina,UMShareToTencent ,UMShareToWechatTimeline,UMShareToQzone,UMShareToRenren,UMShareToEmail,UMShareToLWTimeline,UMShareToYXSession,nil] delegate:nil];
}

- (void)getRequest
{
    [LORequestManger GET:self.urlString success:^(id response) {
        NSDictionary *bigDic = (NSDictionary *)response;
        NSDictionary *smallDic = bigDic[self.docid];
        self.YiDongModel = [YiDongWebViewModel modelWithDict:smallDic];
        [self showInWebView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        [ProgressHUD progressHUD:self.view labelText:@"加载失败"];
    }];
}

- (void)showInWebView
{
    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"SXDetails.css" withExtension:nil]];
    [html appendString:@"</head>"];
    [html appendString:@"<body>"];
    [html appendString:[self touchBody]];
    [html appendString:@"</body>"];
    [html appendString:@"</html>"];
    [self.webView loadHTMLString:html baseURL:nil];
}

- (NSString *)touchBody
{
    NSMutableString *body = [NSMutableString string];
    [body appendFormat:@"<div class=\"title\">%@</div>",self.YiDongModel.title];
    [body appendFormat:@"<div class=\"time\">%@</div>",self.YiDongModel.ptime];
    if (self.YiDongModel.body != nil) {
        [body appendString:self.YiDongModel.body];
    }
    // 遍历img
    for (YiDongImgModel *model in self.YiDongModel.img) {
        NSMutableString *imgHtml = [NSMutableString string];
        
        // 设置img的div
        [imgHtml appendString:@"<div class=\"img-parent\">"];
        
        // 数组存放被切割的像素
        NSArray *pixel = [model.pixel componentsSeparatedByString:@"*"];
        CGFloat width = [[pixel firstObject]floatValue];
        CGFloat height = [[pixel lastObject]floatValue];
        // 判断是否超过最大宽度
        CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width * 0.96;
        if (width > maxWidth) {
            height = maxWidth / width * height;
            width = maxWidth;
        }
        NSString *onload = @"this.onclick = function() {"
        "  window.location.href = 'sx:src=' +this.src;"
        "};";
        [imgHtml appendFormat:@"<img onload=\"%@\" width=\"%f\" height=\"%f\" src=\"%@\">",onload,width,height,model.src];
        // 结束标记
        [imgHtml appendString:@"</div>"];
        // 替换标记
        [body replaceOccurrencesOfString:model.ref withString:imgHtml options:NSCaseInsensitiveSearch range:NSMakeRange(0, body.length)];
    }
    return body;
}

#pragma mark - 内容警告提示
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
